/**
 *	客户端处理
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2024-09-09
 */

package com.framerock.server;


import com.framerock.baselib.Patter;
import com.framerock.cache.Cache;
import java.util.HashMap;
import java.util.Map;


public class WebSocketClients {


    private String from     =  "";  //来源

    private int online_PC   = 0, //PC端在线人数
                online_REIM = 0, //REIM客户端在线
                online_MAPP = 0; //手机端/APP在线

    //端类型
    private String USERLX_P = "p",USERLX_R = "r",USERLX_M = "m";


    public Map<String, HttpWebSocket> WebSocketAll = new HashMap<String, HttpWebSocket>();

    public WebSocketClients(String recid){
        if(recid == null)recid = "defrecid";
        this.from = recid;
    }

    /**
     * 各端类型
     * */
    public String getUserlx(String adminid)
    {
        if(adminid == null)return USERLX_R;
        String lx = adminid.substring(0, 1);
        if(Patter.isnumber(lx))lx = USERLX_R;
        return lx;
    }

    /**
     * 获取客户端
     * */
    public HttpWebSocket getClient(String adminid)
    {
        if(adminid == null)return null;
        return WebSocketAll.get(adminid);
    }

    /**
     * 添加客户端
     * */
    public void addClient(String adminid, HttpWebSocket ws)
    {
        if(adminid == null)return;
        String lx = getUserlx(adminid);
        if(lx.equals(USERLX_R))online_REIM++;
        if(lx.equals(USERLX_P))online_PC++;
        if(lx.equals(USERLX_M))online_MAPP++;
        WebSocketAll.put(adminid, ws);
        if(Cache.isOpen())Cache.set(getKey(adminid), HttpServer.getHost());
    }

    public String getKey(String adminid)
    {
        return ""+this.from+"_"+adminid+"";
    }

    /**
     * 移除
     * */
    public void removeClient(String adminid)
    {
        if(adminid == null)return;
        String lx = getUserlx(adminid);
        if(lx.equals(USERLX_R))online_REIM--;
        if(lx.equals(USERLX_P))online_PC--;
        if(lx.equals(USERLX_M))online_MAPP--;
        WebSocketAll.remove(adminid);
        if(Cache.isOpen())Cache.del(getKey(adminid));
    }

    /**
     * 各端在线人员
     * */
    public Map<String, Integer> getOnline()
    {
        Map<String, Integer> da = new HashMap<String, Integer>();
        da.put(USERLX_R, online_REIM);
        da.put(USERLX_P, online_PC);
        da.put(USERLX_M, online_MAPP);
        return da;
    }

    /**
     * 获取在线人员，返回：a1,a2,m1,m2等
     * */
    public String getOnlineUser()
    {
        String bstr = "",adminid,lx;
        int xu = 0;
        online_REIM = 0;
        online_PC   = 0;
        online_MAPP = 0;
        for(Map.Entry<String,HttpWebSocket> entry:WebSocketAll.entrySet()){
            adminid =  entry.getKey();
            if(xu>0)bstr += ",";
            bstr   += adminid;
            xu++;
            lx = getUserlx(adminid);
            if(lx.equals(USERLX_R))online_REIM++;
            if(lx.equals(USERLX_P))online_PC++;
            if(lx.equals(USERLX_M))online_MAPP++;
        }
        return bstr;
    }

    /**
     * 发消息
     * receid 接收人id，多个,分开
     */
    public String send(String receid, String str)
    {
        HttpWebSocket ws;
        String arr[]     = receid.split(","),bstr = "";
        for(int i=0; i<arr.length; i++){
            ws  = getClient(arr[i]);
            if(ws != null){
                bstr += ""+arr[i]+";";
                ws.send(str);
            }
        }
        return bstr;
    }
}
